<template>
  <div id="app" class="wrapper wrapper-content animated fadeInRight v-cloak" v-cloak>
    <div class="row">
      <div class="ibox float-e-margins">
        <div class="ibox-content col-md-12">
          <form id="riverPolicyForm" method="post" class="form m-t" @submit.prevent="save()" accept-charset="UTF-8">
            <input type="hidden" name="id" v-model="item.id"/>
            <input type="hidden" name="token" v-model="item.token"/>
            <input type="hidden" name="newFlag" v-model="newFlag"/>
            <div class="clearfix "></div>
            <div class="col-md-6 b-r">

              <div class="form-group clearfix">
                <label class="col-md-8 control-label">上传文件：</label>
                <div class="col-md-12">
                  <div class="col-md-8">
                    <input id="bean-name" name="name" v-model="item.name" class="form-control" type="text" readonly
                        required="" aria-required="true" aria-describedby="bean-name-error" maxlength="55"
                        aria-invalid="true">
                  </div>
                  <span class="help-block m-b-none inline">
                    <a href="javascript:;" id="upload_btn_path"
                        class="btn btn-success btn-sm"><i class="fa fa-upload fa-coordinate"></i> 点击上传</a>
                  </span>
                  <div id="upload_div_path" style="">
                    <div class="img-grid-2">
                      <input name="path" id="path" v-model="item.path" type="hidden">
                    </div>
                  </div>
                  <span id="bean-name-error" class="help-block m-b-none" for="bean-name"></span>
                </div>
              </div>

              <div class="form-group clearfix">
                <label class="col-md-8 control-label">河道名称：</label>
                <div class="col-md-12">
                  <select data-placeholder="选择河道名称" id="bean-riverId" name="riverId" class="form-control" required=""
                      aria-required="true" aria-describedby="bean-riverId-error" aria-invalid="true">
                    <option value="">--请选择河道名称--</option>
                    <option v-if="rivers != null && rivers.length > 0"
                        v-for="(river, index) in rivers" :value="river.id"
                        :selected="river.id == item.riverId">{{ river.name }}
                    </option>
                  </select>
                  <span id="bean-riverId-error" class="help-block m-b-none" for="bean-riverId"></span>
                </div>
              </div>

            </div>
            <div class="col-md-6">

              <div class="form-group clearfix">
                <label class="col-md-8 control-label">文件类型：</label>
                <div class="col-md-12">
                  <select data-placeholder="选择件类型" id="bean-type" name="type" class="form-control" required=""
                      aria-required="true" aria-describedby="bean-type-error" aria-invalid="true">
                    <option value="">--请选择件类型--</option>
                    <option value="1" :selected="item.type==1">整治方案</option>
                    <option value="2" :selected="item.type==2">应急预案</option>
                  </select>
                  <span id="bean-type-error" class="help-block m-b-none" for="bean-type"></span>
                </div>
              </div>

              <div class="form-group clearfix">
                <label class="col-md-8 control-label">备注：</label>
                <div class="col-md-12">
                  <textarea id="bean-memo" name="memo" class="form-control" placeholder="请输入备注"
                      aria-describedby="bean-memo-error" maxlength="255"
                      aria-invalid="true">{{item.memo}}</textarea>
                  <span id="bean-memo-error" class="help-block m-b-none" for="bean-memo"></span>
                </div>
              </div>
            </div>

            <div class="clearfix "></div>
            <div class="col-12 text-center" style="height: 50px;">
              <div>
                <input class="btn btn-primary" type="submit" value="保存"/> | <a @click="lbox.closeMyBoxLayer()" class="btn btn-danger" href="javascript:">返回</a>
              </div>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</template>
<script type="text/javascript">
import '@/assets/css/style.css'

import $ from '@/assets/js/jquery-vendor.js'
import 'jquery.cookie'
import axios from 'axios'
import '@/assets/js/validate/validation-vendor.js'
import '@/assets/css/upload.css'
import '@/assets/js/upload_single_file.js'
import {apiUtil, axiosContentType, site} from '@/assets/js/boss'

export default {
  data() {
    return {
      item: {},
      rivers: [],
      newFlag: 0,
      uuidToken: ''
    }
  },
  mounted() {
    let that = window.$vueApp = this;
    document.getElementsByTagName('body')[0].className = 'fixed-sidebar full-height-layout';
    $('#riverPolicyForm').validate();
    that.staff.init(function () {
      that.newFlag = sessionStorage.getItem(site.riverPolicy.info);
      if (that.newFlag) that.uuidToken = apiUtil.guid();
      if (apiUtil.existSessionKey(site.riverPolicy.info)) {
        that.info();
      }
    });
    that.riverList();
    window.upload_single_file('upload_btn_path', 'upload_div_path', 'png,jpg,gif,bmp,xls,xslx,doc,docx,ppt,pptx,pdf,zip,7z,rar,html', 'path', function (data) {
      $('#bean-name').val(data.name);
      $('#path').val(data.url);
      that.item.name = data.name;
      that.item.path = data.url;
    });
  },
  methods: {
    info() {
      let that = this;
      axios.get(site.riverPolicy.info + sessionStorage.getItem(site.riverPolicy.info), {}).then(function (response) {
        const result = response.data;
        if (result.code === 0) {
          that.item = result.data;
        } else {
          alert(result.message);
        }
      });
    },
    riverList() {
      let that = this;
      axios.get(site.riverInfo.list, {}).then(function (response) {
        var result = response.data;
        if (result.code === 0) {
          that.rivers = result.data;
        } else {
          alert(result.message);
        }
      });
    },
    save() {
      let that = this;
      if ($('#riverPolicyForm').valid()) {
        axios.post(site.riverPolicy.save, $('#riverPolicyForm').serialize(),
            axiosContentType.xWwwFormUrlencoded).then(function (response) {
          const result = response.data;
          // console.info("获取数据.." + JSON.stringify(result));
          if (result.code === 0) {
            parent.$vueApp.page();
            alert('保存成功!');
            that.lbox.closeMyBoxLayer()
          } else {
            alert(result.message);
          }
        });
      }
      ;
    }
  }
}
</script>

